import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:refresh_page/src/typedefs.dart';

/// 全局刷新页面配置
class RefreshPageConfiguration extends InheritedWidget {
  final LoadingBuilder? loadingBuilder;
  final EmptyBuilder? emptyBuilder;
  final ErrorBuilder? errorBuilder;
  final Widget? header;
  final Widget? footer;

  const RefreshPageConfiguration({
    super.key,
    this.loadingBuilder,
    this.emptyBuilder,
    this.errorBuilder,
    this.header,
    this.footer,
    required super.child,
  });

  static RefreshPageConfiguration? of(BuildContext context) {
    return context
        .dependOnInheritedWidgetOfExactType<RefreshPageConfiguration>();
  }

  @override
  bool updateShouldNotify(RefreshPageConfiguration oldWidget) {
    return loadingBuilder != oldWidget.loadingBuilder ||
        emptyBuilder != oldWidget.emptyBuilder ||
        errorBuilder != oldWidget.errorBuilder ||
        header != oldWidget.header ||
        footer != oldWidget.footer;
  }

  /// 提供一个简单的初始化方法，可以与其他 builder 组合使用
  static TransitionBuilder init({
    TransitionBuilder? builder,
    LoadingBuilder? loadingBuilder,
    EmptyBuilder? emptyBuilder,
    ErrorBuilder? errorBuilder,
    Widget? header,
    Widget? footer,
  }) {
    return (BuildContext context, Widget? child) {
      if (child == null) return const SizedBox.shrink();

      final configWidget = RefreshPageConfiguration(
        loadingBuilder: loadingBuilder,
        emptyBuilder: emptyBuilder,
        errorBuilder: errorBuilder,
        header: header ?? const ClassicHeader(),
        footer: footer ?? const ClassicFooter(),
        child: child,
      );

      return builder == null ? configWidget : builder(context, configWidget);
    };
  }
}

/// 使用示例:
/// 
/// ```dart
/// MaterialApp(
///   title: 'Refresh Page Demo',
///   home: HomePage(),
///   builder: RefreshPageConfiguration.init(
///     loadingBuilder: (context, controller) => const Center(
///       child: CircularProgressIndicator(),
///     ),
///     emptyBuilder: (context, controller) => const Center(
///       child: Text('暂无数据'),
///     ),
///     errorBuilder: (context, controller, reload) => Center(
///       child: Column(
///         mainAxisSize: MainAxisSize.min,
///         children: [
///           const Text('加载失败'),
///           ElevatedButton(
///             onPressed: reload,
///             child: const Text('重新加载'),
///           ),
///         ],
///       ),
///     ),
///     header: const WaterDropHeader(),
///     footer: const ClassicFooter(),
///   ),
/// )
/// ```
///
/// 与其他 builder 组合使用:
/// 
/// ```dart
/// MaterialApp(
///   builder: RefreshPageConfiguration.init(
///     builder: FlutterSmartDialog.init(),
///     loadingBuilder: (context, controller) => const Center(
///       child: CircularProgressIndicator(),
///     ),
///     emptyBuilder: (context, controller) => const Center(
///       child: Text('暂无数据'),
///     ),
///     errorBuilder: (context, controller, reload) => Center(
///       child: ElevatedButton(
///         onPressed: reload,
///         child: const Text('重新加载'),
///       ),
///     ),
///   ),
/// )
/// ```
///
/// 单个页面自定义示例:
/// 
/// ```dart
/// RefreshPage<MyController>(
///   loadingBuilder: (context, controller) => const CustomLoadingWidget(),
///   emptyBuilder: (context, controller) => const CustomEmptyWidget(),
///   errorBuilder: (context, controller, reload) => CustomErrorWidget(onRetry: reload),
///   // 自定义状态显示位置，不覆盖整个区域
///   emptyOverlay: false,
///   errorOverlay: false,
///   statePositionBuilder: (stateWidget) {
///     return Column(
///       children: [
///         const UserInfoHeader(), // 用户信息区域不被空状态覆盖
///         Expanded(child: stateWidget), // 空状态或错误状态只显示在内容区域
///       ],
///     );
///   },
///   childBuilder: (controller) {
///     return ListView(
///       children: [
///         const UserInfoHeader(),
///         ...controller.data.map((item) => ItemWidget(item: item)).toList(),
///       ],
///     );
///   },
/// )
/// ```
